package com.woniu.dating.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.common.constant.RedisKeyConstant;
import com.woniu.dating.constant.DatingConstant;
import com.woniu.dating.pojo.DatingHobby;
import com.woniu.dating.mapper.DatingHobbyMapper;
import com.woniu.dating.service.IDatingHobbyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.dating.vo.DatingLoginVO;
import com.woniu.dating.vo.RecommendVO;
import com.woniu.user.feignClient.UserFeignClient;
import com.woniu.user.pojo.User;
import com.woniu.user.vo.UserLoginVO;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 玖柒
 * @since 2023-05-20
 */
@Service
public class DatingHobbyServiceImpl extends ServiceImpl<DatingHobbyMapper, DatingHobby> implements IDatingHobbyService {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private UserFeignClient userFeignClient;

    @Resource
    private DatingHobbyMapper datingHobbyMapper;

    /**
     * 初始化用户相亲信息
     * @param userLoginVO
     */
    @Override
    public void createHobby(UserLoginVO userLoginVO) {
        //1.远程调用，查询出用户的信息.
        User user = userFeignClient.getUser(userLoginVO.getId()).getData();
        //2.将必要信息封装到兴趣信息对象中
        DatingHobby datingHobby = new DatingHobby();
        datingHobby.setAvatar(user.getAvatar());
        datingHobby.setMobile(user.getMobile());
        datingHobby.setUserId(userLoginVO.getId());
        datingHobby.setGender(String.valueOf(user.getGender()));
        //3.将新增对象加入数据库中
        datingHobbyMapper.insert(datingHobby);
        //4.生成对应的RedisKey,表示该对象已创建
        redisTemplate.opsForValue().set(RedisKeyConstant.DATING_CREATE+userLoginVO.getId(),userLoginVO.getId());
    }

    /**
     * 根据id查询用户兴趣信息
     * @param userId
     * @return
     */
    @Override
    public DatingHobby getDatingHobbyById(Integer userId) {
        DatingHobby datingHobby = datingHobbyMapper.getDatingHobby(userId);
        return datingHobby;
    }

    /**
     * 查询出所有的相亲兴趣，按照关注度从高到低排序
     */
    @Override
    public List<RecommendVO> listRecommendInfo() {
        List<RecommendVO> listRecommendVO = datingHobbyMapper.listRecommendInfo();
        return listRecommendVO;
    }

    /**
     * 查询出同乡的相亲兴趣信息，按照关注度排序
     */
    @Override
    public List<RecommendVO> listFellow(DatingLoginVO datingLoginVO) {
        List<RecommendVO> listRecommendVO = datingHobbyMapper.listFellow(datingLoginVO.getCity());
        return listRecommendVO;
    }

    /**
     * 查询出所有男性，按照关注度排序
     */
    @Override
    public List<RecommendVO> listBoy() {
        List<RecommendVO> listRecommendVO = datingHobbyMapper.listBoy(DatingConstant.DATING_BOY);
        return listRecommendVO;
    }

    /**
     * 查询出所有女性，按照关注度排序
     */
    @Override
    public List<RecommendVO> listGirl() {
        List<RecommendVO> listRecommendVO = datingHobbyMapper.listGirl(DatingConstant.DATING_GIRL);
        return listRecommendVO;
    }

    /**
     * 根据用户id,查询信息
     */
    @Override
    public RecommendVO getUserDatingHobby(Integer userId) {
        RecommendVO recommendVO = datingHobbyMapper.getDatingHobbyByUserId(userId);
        return recommendVO;
    }

    /**
     * 手机号，获取用户信息
     */
    @Override
    public DatingLoginVO getDatingHobbyByMobile(String mobile) {
        DatingLoginVO datingLoginVO = datingHobbyMapper.getDatingHobbyByMobile(mobile);
        return datingLoginVO;
    }
}
